package controller;

import dao.OrderDao;
import dao.impl.OrderDaoImpl;
import domain.*;
import service.OrderService;
import service.UserService;
import service.impl.OrderServiceImpl;
import service.impl.UserServiceImpl;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseController {
    private OrderService orderService = new OrderServiceImpl();
    private OrderDao orderDao = new OrderDaoImpl();
    private UserService userService = new UserServiceImpl();

    /**
     * 生成订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        Integer userId = user.getId();
        //生成订单号
        String orderId = orderService.createOrder(cart, userId);

        req.getSession().setAttribute("orderId", orderId);
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");
    }

    /**
     * 展示所有的订单（超链接：我的订单）
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        Integer userId = user.getId();
        List<Order> orders = orderService.showMyOrders(userId);
        req.getSession().setAttribute("orders", orders);
        resp.sendRedirect(req.getContextPath() + "/pages/order/order.jsp");
    }

    /**
     * 发货
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Order> orders = orderService.showAllOrders();
        req.getSession().setAttribute("orders", orders);
        String orderId = req.getParameter("orderId");
        orderService.sendOrder(orderId);
        resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showAllOrders");
    }

    /**
     * 展示全部订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Order> orders = orderService.showAllOrders();
        req.getSession().setAttribute("orders", orders);
        resp.sendRedirect(req.getContextPath() + "/pages/order/order.jsp");
    }

    /**
     * 展示订单详情
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        int status = WebUtils.parseInt(req.getParameter("status"), 0);
        int m = WebUtils.parseInt(req.getParameter("m"), 0);
        String username = (String) req.getAttribute("username");
        req.setAttribute("username", username);

        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);

        req.getSession().setAttribute("orderItems", orderItems);

        //m(如果是1，表示是管理员)
        if (m == 1) {
            req.getRequestDispatcher("/pages/order/order_detail.jsp?status=" + status + "&m=" + 1 + "&orderId=" + orderId + "&username=" + username).forward(req, resp);
        } else {
            req.getRequestDispatcher("/pages/order/order_detail.jsp?status=" + status + "&orderId=" + orderId + "&username=" + username).forward(req, resp);
        }
    }

    /**
     * 签收订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void receiveOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        orderService.receiveOrder(orderId);
        resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showOrderDetail&status=" + 2 + "&orderId=" + orderId);
    }

    /**
     * 通过订单号查询用户名（将t_order与t_user连接连接查询）
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryUsernameByOrderId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        String status = req.getParameter("status");
        String m = req.getParameter("m");
        Object username = orderService.queryUsernameByOrderId(orderId);
        req.setAttribute("username", username);
        System.out.println("queryUsernameByOrderId    " + username);
        req.getRequestDispatcher("/orderServlet?action=showOrderDetail&orderId=" + orderId + "&status=" + status + "&m=" + m).forward(req, resp);
    }

    /**
     * 对订单进行评价
     */
    protected void userScore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("uid"));
        String score = req.getParameter("score");
        int i = orderDao.insertUserScore(id, score);
        if (i > 0) {
            req.getRequestDispatcher("/orderServlet?action=selectUserScope&id="+id).forward(req, resp);
        }
    }


    protected void selectUserScope(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserScore userScore = orderDao.selectUserScope(Integer.valueOf(req.getParameter("id")));
        req.getSession().setAttribute("userScore",userScore);
        resp.sendRedirect("/HfsSale/pages/order/order.jsp");
    }


    protected void selectAllUserScore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<UserScore> userScores = orderDao.selectAllUserScore();
        req.setAttribute("allUserScore",userScores);
        req.getRequestDispatcher("/pages/admin/allUserScope.jsp").forward(req,resp);
    }
}
